//
//  ZGLSingleDetCollectionViewCell.m
//  Faker
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLSingleDetCollectionViewCell.h"
#import "ZGLSubDetModel.h"

@interface ZGLSingleDetCollectionViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *fromLabel;

@end

@implementation ZGLSingleDetCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.borderWidth = 0.3f;
        self.titleLabel = [[UILabel alloc] init];
        self.fromLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.fromLabel];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.backgroundColor = foreColor;
    [self.titleLabel setFrame:CGRectMake(ScaleX * 20, ScaleY * 20, ScaleX * 335, ScaleY * 20)];
    [self.fromLabel setFrame:CGRectMake(ScaleX * 20, ScaleY * 50, ScaleX * 160, ScaleY * 15)];
    self.titleLabel.textColor = artiColor;
    self.titleLabel.font = [UIFont systemFontOfSize:17];
    self.fromLabel.font = [UIFont systemFontOfSize:13];
    self.fromLabel.textColor = [UIColor lightGrayColor];
}
- (void)setSubDetModel:(ZGLSubDetModel *)subDetModel {
    if (_subDetModel != subDetModel) {
        _subDetModel = subDetModel;
    }
    self.titleLabel.text = subDetModel.title;
    if (subDetModel.auther_name.length == 0) {
        self.fromLabel.text = [NSString stringWithFormat:@"Faker %@", [self getTimeDifferentWith:subDetModel.date]];
    } else {
        self.fromLabel.text = [NSString stringWithFormat:@"%@ %@", subDetModel.auther_name, [self getTimeDifferentWith:subDetModel.date]];
    }
}

- (NSString *)getNowTime {
    NSDate *senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *locationString=[dateformatter stringFromDate:senddate];
    return locationString;
}
- (NSString *)getTimeDifferentWith:(NSString *)date {
    if (date.length == 0) {
        date = [self getNowTime];
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    //设置时区,这个对于时间的处理有时很重要
    //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
    //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
    //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    NSDate *dateModel = [formatter dateFromString:date];
    NSDate *dateNow = [formatter dateFromString:[self getNowTime]];
    NSString *timeModel = [NSString stringWithFormat:@"%ld", (long)[dateModel timeIntervalSince1970]];
    NSString *timeNow = [NSString stringWithFormat:@"%ld", (long)[dateNow timeIntervalSince1970]];
    NSInteger time = ([timeNow integerValue] - [timeModel integerValue]) / 60;
    if (time > 60) {
        return [NSString stringWithFormat:@"%ld小时前",time / 60];
    } else {
        return [NSString stringWithFormat:@"%ld分钟前",time % 60];
    }
  }


@end
